package com.study.jwt.api;

import com.alibaba.fastjson.JSONObject;
import com.study.jwt.annation.UserLoginToken;
import com.study.jwt.bean.User;
import com.study.jwt.service.TokenService;
import com.study.jwt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: Leo
 * @createDate: 2020/4/27
 * @version: 1.0
 */
@RestController
@RequestMapping("/api")
public class UserApi
{
    @Autowired
    UserService userService;
    @Autowired
    TokenService tokenService;

    //登录
    @PostMapping("/login")
    public Object login(User user)
    {
        JSONObject jsonObject = new JSONObject();
        User userForBase = userService.findByUsername(user);
        if (userForBase == null)
        {
            jsonObject.put("message", "登录失败,用户不存在");
            return jsonObject;
        }
        else
        {
            if (!userForBase.getPassword().equals(user.getPassword()))
            {
                jsonObject.put("message", "登录失败,密码错误");
                return jsonObject;
            }
            else
            {
                String token = tokenService.getToken(userForBase);
                jsonObject.put("token", token);
                jsonObject.put("user", userForBase);
                return jsonObject;
            }
        }
    }

    @UserLoginToken
    @GetMapping("/getMessage")
    public String getMessage()
    {
        return "你已通过验证";
    }
}
